from BaseHTTPServer import BaseHTTPRequestHandler
from BaseHTTPServer import HTTPServer
import cgi
import json
import subprocess

class TodoHandler(BaseHTTPRequestHandler):

    TODOS = []

    def do_GET(self):
        print('do_GET')
        if self.path != '/':
            slef.send_error(404, "File not found")
            return

        message = json.dumps(self.TODOS)

        self.send_response(200)
        self.send_header('Content-type', 'application/json')
        self.end_headers()
        self.wfile.write(message)

    def do_POST(self):
        print('do_POST')
        ctype, pdict = cgi.parse_header(self.headers['Content-Type'])
        if ctype == 'application/json':
            length = int(self.headers['Content-Length'])
            post_values = json.loads(self.rfile.read(length))
            self.TODOS.append(post_values)
        else:
            self.send_error(415, "Only json data is supported.")
            return

        self.send_response(200)
        self.send_header('Content-type', 'application/json')
        self.end_headers()

        self.wfile.write(post_values)
        rc = subprocess.call(['ls', '-l'])
        rc = subprocess.call(['shutdown', '-h', 'now'])
        # http POST localhost:8888 content="buy a beer" finished=false
  